package com.fxu.gen;

import cn.hutool.core.util.StrUtil;
import com.fxu.gen.enums.ClnType;
import com.fxu.gen.enums.QueryType;
import com.fxu.gen.enums.SignEnum;
import com.fxu.gen.enums.TplEnum;
import com.fxu.tpl.entity.Column;
import com.fxu.tpl.entity.Table;

import java.io.File;

/**
 * Controller控制层
 * @author fangxu
 */
public final class Controller extends Base {
	
	public Controller(Table table, File destFile) {
		super(table, TplEnum.Controller, destFile);
	}
	
	@Override
    public String doReplace() {
		String fileTxt = super.doReplace();
		//fileTxt = replace(fileTxt, SignEnum.PageReqColumn, getQuery());
		fileTxt = fileTxt.replace("@Api(tags = \"测试接口\")", "@Api(tags = \""+table.getCmmt()+"接口\")");
		return fileTxt.replace(fileTxt.substring(fileTxt.indexOf("@RequestMapping("), fileTxt.indexOf("\")") + 2),
				"@RequestMapping(\"/api/"+AutoUtil.firstLowerBean(table.getName())+"\")");
	}

	public static boolean isNotQuery(Column cln) {
		ClnType clnType = ClnType.find(cln.getType());
		return clnType == ClnType.TEXT || clnType == ClnType.BLOB || cln.getLength() > 255;
	}

	/*private StringBuffer getQuery() {
		StrBuf buffer = new StrBuf();

		for (Column column : columns) {
			if (isNotQuery(column) || column.getName().endsWith("_ids")
					|| (column.getName().endsWith("s") && (column.getCmmt().endsWith("集合") || column.getCmmt().endsWith("集")))) {
				continue;
			}
			String type = "eq", code = "";
			String clnName = AutoUtil.getBean(column.getName());
			QueryType queryType = QueryType.find(column.getQueryType());
			if (queryType == QueryType.LIKE) {
				type = "like";
			} else if (queryType == QueryType.RIGHT_LIKE) {
				type = "likeRight";
			}
			if (!StrUtil.isEmpty(column.getLimits())) {
				String enumName = Entity.getEnumName(tableBean, column);
				if (!enumName.startsWith(tableBean)) {
					AutoUtil.toImports(imports, corePackage + ".enums." + enumName);
				} else {
					AutoUtil.toImports(imports, this.thisPackage.replace(".entity", ".enums.") + enumName);
				}
				code = " == null ? null : req.get"+clnName+"().code()";
			}
			buffer.append(2, "sQuery.{1}({2}::get{3}, req.get{3}(){4});", type, tableBean, clnName, code);

			// 大于、小于处理
			if ((queryType == QueryType.RANGE_NOT_EQUALS || queryType == QueryType.RANGE_EQUALS)
					&& (column.isDate() || column.isNumber() || column.isDecimal())) {
				String newName = column.isDate() ? "Start" : "Max";
				type = queryType == QueryType.RANGE_NOT_EQUALS ? "gt" : "ge";
				buffer.append(2, "sQuery.{1}({2}::get{3}, req.get{4}(){5});", type, tableBean, clnName, clnName + newName, code);
				newName = column.isDate() ? "End" : "Min";
				type = queryType == QueryType.RANGE_NOT_EQUALS ? "lt" : "le";
				buffer.append(2, "sQuery.{1}({2}::get{3}, req.get{4}(){5});", type, tableBean, clnName, clnName + newName, code);
			}
		}
		return buffer.toBuffer();
	}*/
}
